export const description =
  'Configure timeout settings to handle high-concurrency scenarios and prevent resource exhaustion.'

# Timeout Configuration

Robyn supports comprehensive timeout configuration to handle high-concurrency scenarios and prevent resource exhaustion like the "Too many open files" error. {{ className: 'lead' }}

## Configuration Options

### Method Parameters

Configure timeouts directly in the `app.start()` method:

```python
from robyn import Robyn

app = Robyn(__file__)

@app.get("/")
async def hello(request):
    return "Hello, world!"

# Configure timeout settings
app.start(
    host="0.0.0.0", 
    port=8080,
    client_timeout=30,          # Client connection timeout (seconds)
    keep_alive_timeout=20       # Keep-alive timeout (seconds)
)
```

### Environment Variables

Override configuration using environment variables:

```bash
# Set timeout configurations
export ROBYN_CLIENT_TIMEOUT=45
export ROBYN_KEEP_ALIVE_TIMEOUT=30

# Start your application
python app.py
```

## Configuration Parameters

| Parameter | Default | Description | Environment Variable |
|-----------|---------|-------------|---------------------|
| `client_timeout` | 30 | Maximum time (seconds) for client request processing | `ROBYN_CLIENT_TIMEOUT` |
| `keep_alive_timeout` | 20 | Time (seconds) to keep idle connections alive | `ROBYN_KEEP_ALIVE_TIMEOUT` |

## Usage Examples

### Basic Configuration

```python
# Minimal timeout configuration
app.start(client_timeout=30)
```

### High-Traffic Production Setup

```python
# Optimized for high-traffic scenarios
app.start(
    host="0.0.0.0",
    port=8080,
    client_timeout=60,      # Allow longer processing time
    keep_alive_timeout=15   # Shorter keep-alive for faster turnover
)
```

### Development Setup

```python
# Development-friendly settings
app.start(
    client_timeout=300,     # Long timeout for debugging
    keep_alive_timeout=60   # Longer keep-alive for testing
)
```

### Load Testing Configuration

```python
# Optimized for load testing with tools like wrk
app.start(
    client_timeout=10,      # Quick timeouts
    keep_alive_timeout=5    # Fast connection turnover
)
```

## Environment Variable Priority

Environment variables take precedence over method parameters:

```python
# This will use ROBYN_CLIENT_TIMEOUT=60 if set, otherwise 30
app.start(client_timeout=30)
```

## Troubleshooting

### "Too Many Open Files" Error

If you encounter file descriptor exhaustion:

1. **Increase system limits:**
   ```bash
   ulimit -n 65536
   ```

2. **Optimize timeout settings:**
   ```python
   app.start(
       client_timeout=15,      # Shorter timeouts
       keep_alive_timeout=5    # Faster connection cleanup
   )
   ```

3. **Use environment variables for deployment:**
   ```bash
   export ROBYN_CLIENT_TIMEOUT=15
   export ROBYN_KEEP_ALIVE_TIMEOUT=5
   ```

### Performance Tuning

**For high-throughput APIs:**
- Lower `keep_alive_timeout` (5-15s)
- Moderate `client_timeout` (15-30s)

**For long-running operations:**
- Higher `client_timeout` (60-300s)
- Standard `keep_alive_timeout` (20-30s)


## Best Practices

1. **Always set explicit timeouts** in production
2. **Use environment variables** for deployment-specific configuration
3. **Test timeout settings** with realistic load patterns
4. **Start with conservative values** and tune based on metrics

## Migration Guide

### From Previous Versions

If upgrading from earlier Robyn versions, the default behavior changes:

**Before (infinite timeout):**
```python
# Previously: no timeout (could cause resource exhaustion)
app.start(host="0.0.0.0", port=8080)
```

**After (sensible defaults):**
```python
# Now: automatic 30s client timeout, 20s keep-alive
app.start(host="0.0.0.0", port=8080)
# Equivalent to:
app.start(
    host="0.0.0.0", 
    port=8080,
    client_timeout=30,
    keep_alive_timeout=20
)
```